package com.standardoperationapp.operationplan;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Vibrator;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.view.ContextThemeWrapper;

import com.standardoperationapp.R;
import com.standardoperationapp.app.BaseApplication;
import com.standardoperationapp.global.GlobalVaries;
import com.standardoperationapp.net.OnSuccessAndFaultListener;
import com.standardoperationapp.net.OnSuccessAndFaultSub;
import com.standardoperationapp.net.OperationApi;
import com.standardoperationapp.utils.ToastUtil;

import java.util.Objects;

public class AlarmService extends Service {

    public static String ACTION_ALARM = "action_alarm";
    private Handler mHanler = new Handler(Looper.getMainLooper());

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        warning();
        mHanler.post(() -> {
            Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(5* 1000); //震动5秒钟

            // 获取默认的提示音
            Ringtone ringtone = RingtoneManager.getRingtone(AlarmService.this, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
            // 如果正在播放，先停止
            ringtone.stop();
            // 播放提示音
            ringtone.play();
//            AlertDialog dialog = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.dialog))
//                    .setTitle("报警")
//                    .setMessage("5分钟超时了！！")
//                    .setPositiveButton("知道了", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialogInterface, int i) {
//                            dialogInterface.cancel();
//                            vibrator.cancel();
//                            ringtone.stop();
//                        }
//                    }).create();
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0新特性
//                Objects.requireNonNull(dialog.getWindow()).setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY  );
//            } else {
//                Objects.requireNonNull(dialog.getWindow()).setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
//            }
//            dialog.show();
        });
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 报警
     * @return
     */
    private void warning() {
        OperationApi.recordJointControlWarn(GlobalVaries.executableId, GlobalVaries.workPointId, GlobalVaries.plantId,  new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
            @Override
            public void onSuccess(Object result) {
                ToastUtil.showToast("5秒钟未操作，已报警");
            }

            @Override
            public void onFault(String errorMsg) {
                ToastUtil.showToast(errorMsg);
            }
        }));
    }

}
